<template>
  <div>
    <div v-if="show" class="cus">
      <div class="cus_box" style="width:1200px;">
        <div class="title">
          <span>关联设备</span>
          <i @click="show = false" class="el-icon-close"></i>
        </div>

        <!--        <div class="mt10">-->
        <!--          <el-input style="width:120px;" placeholder="广告名称"></el-input>-->

        <!--          <el-button type="primary">搜索</el-button>-->
        <!--        </div>-->
        <div class="pt24">
          <el-button :type="activeName == 1 ? 'primary' : 'default'" @click="handleClick(1)">换电柜
          </el-button>
          <el-button :type="activeName == 3 ? 'primary' : 'default'" @click="handleClick(3)">充电柜
          </el-button>
        </div>

        <div class="content">
          <el-table :data="tableData" height="500" border @selection-change="handleSelectionChange">
            <el-table-column align="center" type="selection" width="55"></el-table-column>
            <el-table-column align="center" prop="id" label="ID" width="180"></el-table-column>
            <el-table-column align="center" prop="sn" label="机柜编号" width="180"></el-table-column>
            <el-table-column align="center" prop="deviceType" label="设备类型" width="180">
              <template slot-scope="scope">
                {{scope.row['deviceType'] == 1?'换电柜':scope.row['deviceType'] == 2?'电动车':scope.row['deviceType'] == 3?'充电柜':'--'}}
              </template>
            </el-table-column>
            <el-table-column align="center" label="二维码">
              <template slot-scope="scope">
                <el-popover placement="right" trigger="hover">
                  <el-image style="width:300px;height:300px;" :src="baseUrl + scope.row.qrCode">
                  </el-image>
                  <el-image slot="reference" :src="baseUrl + scope.row.qrCode"></el-image>
                </el-popover>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="deviceName" label="机柜名称"></el-table-column>
            <el-table-column align="center" prop="address" label="地址"></el-table-column>
            <el-table-column align="center" prop="agentName" label="管理代理商"></el-table-column>
            <el-table-column align="center" prop="deviceStatus" label="机柜状态">
              <!-- 设备状态：1：（在线）运行中 2: 离线 3: 设备故障 -->
              <template slot-scope="scope">
                <span
                  :class="scope.row['deviceStatus'] == 1 ? 'success' : scope.row['deviceStatus'] == 2 ? 'c9' : scope.row['deviceStatus'] == 3 ? 'danger' : ''">
                  {{
										scope.row['deviceStatus'] == 1
											? '（在线）运行中'
											: scope.row['deviceStatus'] == 2
											? '离线'
											: scope.row['deviceStatus'] == 3
											? '设备故障'
											: '--'
									}}
                </span>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="ctime" label="创建时间"></el-table-column>
          </el-table>
        </div>

        <div class="btns mt20" style="text-align: center;">
          <el-button @click="show = false">取消</el-button>
          <el-button @click="submit" type="primary">确定</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Related_advertising',
  props: {
    addList: {
      // 已经关联的设备
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      activeName: 1,
      show: false,
      tableData: [],
      data: [],
      row: {},
      deviceList: [] // 所有的设备
    }
  },
  mounted() {
    // this.getData()
  },

  methods: {
    handleClick(activeName) {
      if (activeName == this.activeName) {
        return false
      }
      this.activeName = activeName
      this.getData()
    },
    async getData() {
      const res = await this.apis.device_list({
        pageNum: 1,
        pageSize: 100000,
        deviceType: this.activeName
      })
      let deviceList = res.rows || []

      if (this.addList.length > 0) {
        deviceList = deviceList.filter(e => {
          const index = this.addList.findIndex(dd => dd.deviceNo == e.deviceNo)

          if (index == -1) {
            return true
          }
        })
      }
      this.tableData = deviceList
    },
    fun_show(row) {
      this.row = row
      this.show = true
      this.data = []
      this.getData()
    },
    handleSelectionChange(val) {
      console.log(val)
      this.data = val
    },
    async submit() {
      if (this.data.length == 0) {
        this.$message({
          message: '请选择设备！',
          type: 'error'
        })
        return
      }

      let deviceIdList = []
      let deviceId = ''
      this.data.map(res => {
        deviceIdList.push(res.id)
      })

      deviceId = deviceIdList.join(',')

      const res = await this.apis.advertDevice_save({
        abId: this.row.id,
        deviceId: deviceId
      })

      this.$message({
        message: '操作成功！',
        type: 'success'
      })
      this.show = false
      this.$emit('add_call')
    }
  }
}
</script>

<style scoped lang="scss"></style>
